package MainPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

public class Manager
{
	private final File dataFile = new File("InformationData4.dat");
	HashSet<Information> infoStorage = new HashSet<Information>();

	static Manager inst = null;

	public static Manager createManagerInst()
	{
		if (inst == null)
		{
			inst = new Manager();
		}

		return inst;
	}

	private Manager()
	{
		readFromFile();
	}
	
/*
	private Information readStudentInfo()
	{
		System.out.print("name: ");
		String name = MenuViewer.keyboard.nextLine();

		System.out.print("phone number: ");
		String phone = MenuViewer.keyboard.nextLine();

		System.out.print("registered date(yyyy.mm.dd): ");
		String dateData = MenuViewer.keyboard.nextLine();
		Date date = null;
		try
		{
			DateFormat format = new SimpleDateFormat("yyyy.MM.dd");
			date = format.parse(dateData);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}

		System.out.print("payment: ");
		int money = MenuViewer.keyboard.nextInt();
		MenuViewer.keyboard.nextLine();

		System.out.print("class name(세잔, 고흐, 모네): ");
		String className = MenuViewer.keyboard.nextLine();

		System.out.print("way of payment(카드, 현금): ");
		String wayOfPayment = MenuViewer.keyboard.nextLine();

		return new Information(name, phone, date, date,  money, className, wayOfPayment);
	}*/

	public boolean inputData(Information info) throws Exception
	{
		if(info.hasNullInfo())
		{
			throw new NullInformationException();
		}
		boolean isAdded = infoStorage.add(info);
		if(isAdded == true) return true;
		else throw new Exception();
	}

	public String searchData(String name)
	{
		Information info = search(name);
		if (info == null)
		{
			return null;
		}
		else
		{
			return info.toString();
		}
	}

	/*
	public boolean deleteData(String name)
	{
		Iterator<Information> itr = infoStorage.iterator();
		while (itr.hasNext())
		{
			Information curInfo = itr.next();
			if (name.compareTo(curInfo.name) == 0)
			{
				itr.remove();
				return true;
			}
		}
		return false;
	}*/
	
	public boolean deleteData(Information info)
	{
		Iterator<Information> itr = infoStorage.iterator();
		Information curInfo;
		while(itr.hasNext())
		{
			curInfo = itr.next();
			if(info.equals(curInfo) == true)
			{
				itr.remove();
				return true;
			}
		}
		return false;
	}

	private Information search(String name)
	{
		Iterator<Information> itr = infoStorage.iterator();
		while (itr.hasNext())
		{
			Information curInfo = itr.next();
			if (name.compareTo(curInfo.name) == 0)
			{
				return curInfo;
			}
		}
		return null;
	}

	public void storeToFile()
	{
		try
		{
			FileOutputStream file = new FileOutputStream(dataFile);
			ObjectOutputStream out = new ObjectOutputStream(file);

			Iterator<Information> itr = infoStorage.iterator();
			while (itr.hasNext())
			{
				out.writeObject(itr.next());
			}

			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void readFromFile()
	{
		if (dataFile.exists() == false)
		{
			return;
		}

		try
		{
			FileInputStream file = new FileInputStream(dataFile);
			ObjectInputStream in = new ObjectInputStream(file);

			while (true)
			{
				Information info = (Information) in.readObject();
				if (info == null)
				{
					break;
				}
				infoStorage.add(info);
			}
			in.close();
		}
		catch (Exception e)
		{
			return;
		}
	}
}
